package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.LikeStrategyMapper;
import cn.wolfcode.trip.base.mapper.StrategyMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StrategyServiceImpl implements IStrategyService {

    @Autowired
    private StrategyMapper strategyMapper;
    @Autowired
    private LikeStrategyMapper likeStrategyMapper;

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Strategy> list = strategyMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public Strategy selectByStrategyId(Long id) {
        return strategyMapper.selectByPrimaryKey(id);
    }
    //添加中间
    @Override
    public void insertRelation(Long strategyId, Long travelId) {
        strategyMapper.insertmiddle(strategyId,travelId);
    }
    //删除中间
    @Override
    public void deleteRelation(Long strategyId, Long travelId) {
       strategyMapper.deleteRelation(strategyId,travelId);
    }
    //app获取数据
    @Override
    public Strategy getTravelById(Long id) {
        return strategyMapper.selectByPrimaryKey(id);
    }

    @Override
    public int getLikeCount(Long strategyId) {
        return likeStrategyMapper.queryLikeCount(strategyId);
    }

    @Override
    public int queryIfexist(Long strategyId, Long userId) {
        return likeStrategyMapper.queryIfexist(strategyId,userId);
    }

    @Override
    public void saveLike(Long userId, Long strategyId) {
        likeStrategyMapper.insert(strategyId,userId,new Date());
    }

    @Override
    public void deleteLike(Long userId, Long strategyId) {
        likeStrategyMapper.deleteByPrimaryKey(strategyId,userId);
    }


    /**
     * 根据标题查询对应的攻略
     * @param title
     * @return
     */
    @Override
    public Strategy selectStrategyByTitle(String title) {
        return strategyMapper.selectByStrategyTitle(title);
    }

    @Override
    public List<String> searchTitle() {
        return strategyMapper.selectAllTitle();
    }

    @Override
    public List<Strategy> listAll() {
        return strategyMapper.selectAll();
    }

    @Override
    public void saveOrUpdate(Strategy strategy) {
        if(strategy.getId()==null){
            strategyMapper.insert(strategy);
        }else{
            strategyMapper.updateByPrimaryKey(strategy);
        }
    }

}
